<%@page import="java.util.List"%>
<%@page import="dao.NewsDAO"%>
<%@page import="bean.News"%>
<%@ page import="bean.Admin" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻管理</title>
<link rel="stylesheet" type="text/css" href="../../css/Iframe.css" />
<link rel="stylesheet" type="text/css"
	href="../../css/bootstrap.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../../css/layui.css" />
</head>

<body>
	<%
		Admin admin = (Admin)session.getAttribute("admin");
		if ((session.getAttribute("admin") != null) && !("true".equals((String) session.getAttribute("refresh")))) {
		out.print("<script>parent.location.reload();</script>");
		session.setAttribute("refresh", "true");
	}
	String currentPage = request.getParameter("currentPage");
	if (currentPage == null) {
		currentPage = "1";
	}
	NewsDAO dao = new NewsDAO();
	//总新闻数
	int allRecords = dao.getAllRecords();
	//总页数
	int allPage = dao.getTotalPage(allRecords);
	//当前页面新闻内容
	int currentPageInt = Integer.parseInt(currentPage);
	if (currentPageInt < 1) {
		currentPageInt = 1;
		out.print("<script>alert('已经是第一页了！')</script>");
	} else if (currentPageInt > allPage) {
		currentPageInt = allPage;
		out.print("<script>alert('已经是最后一页了！')</script>");
	}
	List<News> list = dao.getCurrentPageNews(currentPageInt);
	session.setAttribute("newsList", list);
	%>
	<span class="cp_title">新闻管理</span>
	<div class="add_cp">
		<a href="publish.jsp?currentPage=<%=currentPageInt%>">+添加新闻</a>
	</div>

	<div class="table_con">
		<div class="table_con">
			<table>
				<tr class="tb_title">
					<td width="5%">ID</td>
					<td width="12%">标题</td>
					<td width="12%">发布者</td>
					<td width="20%">时间</td>
					<td width="20%">图片</td>
					<%
						if(admin.getAdmin()){%>
					<td width="13%">操作</td>
					<%
						}
					%>
				</tr>
				<%
					for (int i = 0; i < list.size(); i++) {
				%>
				<tr>
					<td width="5%"><%=list.get(i).getId()%></td>
					<td width="12%"><%=list.get(i).getTitle()%></td>
					<td width="12%"><%=list.get(i).getPublisher()%></td>
					<td width="20%"><%=list.get(i).getTime()%></td>
					<td width="20%"><%=list.get(i).isHasImg()?"存在":"不存在"%></td>

					<%
						if(admin.getAdmin()){%>
					<td width="13%"><a
						href="/News/GetNewsByID?index=<%=i%>&currentPage=<%=currentPageInt%>"
						class="del_btn">编辑</a> &nbsp; <a
						href="/News/DeleteNews?id=<%=list.get(i).getId()%>&currentPage=<%=currentPageInt%>"
						class="del_btn">删除</a></td>
				</tr>
				<%}%>
				<%
					}
				%>
			</table>
			<div style="text-align: center">
				一共有<%=allRecords%>条记录，共<%=allPage%>页，当前第<%=currentPageInt%>页&nbsp;
			</div>
			<br>
				<div style="text-align: center">
					<ul class="pagination">
						<li><a href="manageNews.jsp?currentPage=1">首页</a></li>
						<li><a
							href="manageNews.jsp?currentPage=<%=currentPageInt - 1%>">上一页</a></li>
						<li><a
							href="manageNews.jsp?currentPage=<%=currentPageInt + 1%>">下一页</a></li>
						<li><a href="manageNews.jsp?currentPage=<%=allPage%>">尾页</a></li>
					</ul>
				</div> <br>
		</div>
	</div>


</body>
</html>

